package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.GameNumbersResult;
import com.contest.entity.GamingOdds;
import com.contest.entity.UserInfo;
import com.contest.service.GamingOddsService;
import com.contest.service.UserInfoService;
import com.contest.util.DateUtil;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.contest.entity.GameNumbersTransactionRecord;
import com.contest.service.GameNumbersTransactionRecordService;


/**
 * 
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-05-17 14:57:27
 */

@Controller
@RequestMapping("/gameNumbersTransactionRecord")
public class GameNumbersTransactionRecordController {

    @Autowired
    GameNumbersTransactionRecordService gameNumbersTransactionRecordService;
    @Autowired
    IdWorker idWorker;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    GamingOddsService gamingOddsService;


    @PostMapping("/client/listUserDatas")
    @ResponseBody
    public PageUtils listUserDatas(@RequestParam Map<String,Object> param) {
        int page = 1;
        int rows = 10;
        try {
            page = Integer.parseInt(param.get("page").toString());
        }catch (Exception e){
        }
        try {
            rows = Integer.parseInt(param.get("rows").toString());
        }catch (Exception e){
        }
        String userId = (String) param.get("userId");
        Page<GameNumbersTransactionRecord> mPage = new Page<GameNumbersTransactionRecord>(page  , rows);
        Wrapper<GameNumbersTransactionRecord> w = new EntityWrapper<>();
        w.eq("fd_user_id" , userId);
        w.orderBy("fd_date" , false);
        w.orderBy("fd_create_time" , false);
        mPage = gameNumbersTransactionRecordService.selectPage(mPage , w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }


    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<GameNumbersTransactionRecord> mPage = new Page<GameNumbersTransactionRecord>(page  , rows);
        param.remove("page");
        param.remove("rows");
        Wrapper<GameNumbersTransactionRecord> w = new EntityWrapper<>();
        Set<String> keys = param.keySet();
        for (String key : keys) {
            Object value = param.get(key);
            if (value != null && !"".equals(value)) {
                w.like(key, value.toString());
            }
        }
        w.orderBy("fd_date" , false);
        w.orderBy("fd_create_time" , false);
        mPage = gameNumbersTransactionRecordService.selectPage(mPage , w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(GameNumbersTransactionRecord gameNumbersTransactionRecord) {
        gameNumbersTransactionRecord.setFdId(idWorker.nextId() + "");
        if (gameNumbersTransactionRecordService.insert(gameNumbersTransactionRecord)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        GameNumbersTransactionRecord gameNumbersTransactionRecord = gameNumbersTransactionRecordService.selectById(fdId);
        model.addAttribute("gameNumbersTransactionRecord", gameNumbersTransactionRecord);
        return "gameNumbersTransactionRecord/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (gameNumbersTransactionRecordService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(GameNumbersTransactionRecord gameNumbersTransactionRecord) {
        if (gameNumbersTransactionRecordService.updateById(gameNumbersTransactionRecord)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }

    /**
     * 方法名称：下注
     * 概要说明：用户下注
     * @Ather 袁飞龙
     * @Date 2018-6-13 11:32
     * @Param
     * @Return
     */
    @RequestMapping("/client/betting")
    @ResponseBody
    public R betting(GameNumbersTransactionRecord record){
        double bettingAmount =  record.getFdMoney().doubleValue();
        String userId = record.getFdUserId();
        String typeId = record.getFdBettingTypeId();
        GamingOdds gamingOdds = gamingOddsService.selectById(typeId);
        UserInfo userInfo = userInfoService.selectById(userId);
        if(userInfo.getFdOver().doubleValue() < bettingAmount){
            return R.error("账户金币不足!");
        }
        record.setFdId(idWorker.nextId()+"");
        record.setFdUserName(userInfo.getFdNickname());
        record.setFdCreateTime(DateUtil.now());
        record.setFdIsSettle(0);
        record.setFdBettingTypeName(gamingOdds.getFdName());
        record.setFdBettingTypeScale(gamingOdds.getFdOdds());
        double todayAmount = userInfo.getFdTodayAmount().doubleValue();
        userInfo.setFdTodayAmount(BigDecimal.valueOf(todayAmount+bettingAmount));
        userInfoService.updateById(userInfo);
        if(gameNumbersTransactionRecordService.insert(record)){
            return R.ok("下注成功!");
        }else{
            return R.error("系统繁忙!");
        }

    }

}
